package org.lex.reflect.browsing.view.browser;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.Comparator;

import javax.swing.table.AbstractTableModel;

import org.lex.reflect.browsing.util.ClassHelper;


public class FieldTableModel extends AbstractTableModel {
	private static final String[] COLUMN_NAMES = { "Modifier", "Name", "Type" };
	private static final String[][] EMPTY = new String[0][0];
	private String[][] data = null;

	public FieldTableModel() {
		this.data = EMPTY;
	}

	public void setClass(Class<?> clazz) {
		Field[] all = clazz.getDeclaredFields();
		data = new String[all.length][];
		for (int i = 0; i < all.length; i++) {
			Field field = all[i];
			data[i] = new String[3];
			data[i][0] = Modifier.toString(field.getModifiers());
			data[i][1] = field.getName();
			data[i][2] = ClassHelper.getTypeName(field.getType());
		}
		Arrays.sort(data, new Comparator<String[]>() {
			public int compare(String[] o1, String[] o2) {
				return o1[1].compareTo(o2[1]);
			}
		});
		this.fireTableStructureChanged();
	}

	public String getColumnName(int column) {
		return COLUMN_NAMES[column];
	}

	public int getColumnCount() {
		return 3;
	}

	public int getRowCount() {
		return data.length;
	}

	public Object getValueAt(int row, int column) {
		return data[row][column];
	}
}
